package com.glory.gloryquickui.view

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.SingleChooseOptionsBean
import com.glory.gloryquickui.utils.FloatingWindowsUtils
import com.glory.gloryquickui.viewmodel.QuickUIViewModel

/**
 *下拉选择
 */
class DropDowmSelectController(context: Context?,  val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvDropDownSelectName: TextView? = null
    private var tvDropDownSelectValue: TextView? = null
    private var flDropDownSelect: FrameLayout? = null
    private var ivDropDownSelect: ImageView? = null
    private var ivClear: ImageView? = null
    private var textPop: FloatingWindowsUtils.GeneralPop? = null
    private var optionList:ArrayList<SingleChooseOptionsBean.Option> = arrayListOf()
    private var stringList:ArrayList<String> = arrayListOf()
    override fun resLayoutId(): Int {
        return R.layout.layout_drop_down_select
    }

    override fun onBindView(data: FieldBean?) {
        tvDropDownSelectName!!.text = data?.config?.label
        tvDropDownSelectValue!!.hint = data!!.placeholder
        //设置禁用
        flDropDownSelect?.isEnabled = !data.disabled
        setOnclick(data)
    }

    private fun setOnclick(data: FieldBean?) {
            flDropDownSelect!!.setOnClickListener {
                if(data?.config?.option?.dataType == "manual") {
                    stringList.clear()
                    optionList.clear()
                    for (option in data?.config?.option!!.optionList) {
                        if (data?.config?.option?.dependObject!!.isEmpty()) {
                            stringList.add(option.label)
                            optionList.add(option)
                        } else {
                            if (viewModel.pageData.value?.get(data?.config?.option?.dependObject) == option.parentId) {
                                stringList.add(option.label)
                                optionList.add(option)
                            }
                        }
                    }
                    updateDropDowmView()
                }else if (data?.config?.option?.dataType == "dictionary"){
                    viewModel.searchDictionaryInfo(data)
                }else if (data?.config?.option?.dataType == "dataTable"){
                    viewModel.searchDataTableInfo(data)
                }else if (data?.config?.option?.dataType == "third"){
                    viewModel.searchDataThirdInfo(data)
                }
            }
    }

    private fun showClear(clearAble: Boolean) {
        if (clearAble){
            ivDropDownSelect?.setImageDrawable(
                context?.resources?.let {
                    ResourcesCompat.getDrawable(
                        it,
                        R.drawable.clear,
                        null
                    )
                })

            ivDropDownSelect?.setOnClickListener{
                tvDropDownSelectValue?.text = ""
                ivDropDownSelect?.setImageDrawable(
                    context?.resources?.let {
                        ResourcesCompat.getDrawable(
                            it,
                            R.drawable.up_arrow,
                            null
                        )
                    })
            }
        }
    }
    fun updateDropDowmData(dataList:ArrayList<SingleChooseOptionsBean.Option>,stringList:ArrayList<String>){
        this.optionList = dataList
        this.stringList = stringList
        updateDropDowmView()
    }

    fun updateDropDowmView(){
        if(textPop == null){

            textPop = flDropDownSelect?.let { it ->
                if (data?.multiple == true){
                    FloatingWindowsUtils.getManyChoosePop(context, it.width,
                        stringList, ivDropDownSelect, object :
                            FloatingWindowsUtils.PopManyChooseCallback {
                            override fun onChecked(string: String?, potion: Int) {

                                var valueString:String = ""
                                if (tvDropDownSelectValue?.text == ""){
                                    tvDropDownSelectValue?.text = string
                                    valueString = optionList.get(potion).value
                                }else{
                                    tvDropDownSelectValue?.text =
                                        tvDropDownSelectValue?.text.toString()  + ";" + string
                                    valueString = valueString + ";" + optionList.get(potion).value
                                }
                                val map = viewModel.pageData.value!!.toMutableMap()
                                map[data!!.vModel] = valueString
                                viewModel.pageData.value = map
                            }

                            override fun onUnChecked(string: String?, potion: Int) {
                                var valueString:String = ""
                                val split = tvDropDownSelectValue?.text.toString().split(";")
                                tvDropDownSelectValue?.text = ""
                                split.forEach{ str ->
                                    if (str == string){
                                        return@forEach
                                    }
                                    val index = stringList.indexOf(str)

                                    if (tvDropDownSelectValue?.text == ""){
                                        tvDropDownSelectValue?.text = str
                                        valueString = optionList.get(index).value

                                    }else{
                                        tvDropDownSelectValue?.text =
                                            tvDropDownSelectValue?.text.toString()  + ";" + str
                                        valueString = valueString + ";" + optionList.get(index).value
                                    }
                                }
                                val map = viewModel.pageData.value!!.toMutableMap()
                                map[data!!.vModel] = valueString
                                viewModel.pageData.value = map
                            }
                        })
                }else{
                    FloatingWindowsUtils.getOneChoosePop(context, it.width,
                        stringList, ivDropDownSelect, object :
                            FloatingWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(string: String?, potion: Int) {
//                                    //设置某字段禁用
//                                    if (optionList[potion].disabled) {
//                                        Toast.makeText(
//                                            context,
//                                            "当前选项不允许选中，请重新选择",
//                                            Toast.LENGTH_SHORT
//                                        ).show()
//                                        return
//                                    }
                                tvDropDownSelectValue!!.text = string
                                showClear(data?.clearable == true)
                                textPop?.dismiss()
                                val index = stringList.indexOf(string)
                                val map = viewModel.pageData.value!!.toMutableMap()
                                map[data!!.vModel] = optionList.get(index).value
                                viewModel.pageData.value = map
                            }
                        })
                }
            }

        }

        if (textPop!!.isShowing) {
            textPop!!.dismiss()
        } else {
            ivDropDownSelect?.let { FloatingWindowsUtils.setArrowShow(it) }
            textPop!!.showAsDropDown(flDropDownSelect, 0, 0)
        }
    }


    override fun onCreatedView(view: View?) {
        tvDropDownSelectName = view?.findViewById(R.id.tvDropDownSelectName)
        tvDropDownSelectValue = view?.findViewById(R.id.tvDropDownSelectValue)
        flDropDownSelect = view?.findViewById(R.id.flDropDownSelect)
        ivDropDownSelect = view?.findViewById(R.id.ivDropDownSelect)
        ivClear = view?.findViewById(R.id.ivClear)
    }

    override fun masterPart(): View? {
        return tvDropDownSelectValue
    }
}